package com.kkb.controller;

import com.kkb.pojo.Team;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

@Controller
@RequestMapping("result")
public class ResultController {
    //1.返回值是ModelAndView:这种方式既有数据的携带还有资源的跳转，可以选择该种方式
    @RequestMapping("test1.do")
    public ModelAndView test1(){
        ModelAndView mv = new ModelAndView();
        //携带数据
        mv.addObject("teamName","热火");//相当于request.setAttribute("teamName","湖人队“);
        //经过视图解析器InternalResourceViewResolver的处 理，将逻辑视图名称加上前后缀变为物理资源路径/result.jsp
        mv.setViewName("result");

        return mv;
    }


    //2.返回String
    @RequestMapping("test2.do")
    public String test2(HttpServletRequest request){
        Team team = new Team();
        team.setLocation("金州");
        team.setTeamId(1009);
        team.setTeamName("勇士");

        //将数据保存到页面
        request.setAttribute("team",team);

        //经过视图解析器InternalResourceViewResolver的处理，将逻辑视图名称加上前后缀变为物理资源路径/jsp/result.jsp
        return "result";

    }

    //3.返回对象类型
    /*
    * 返回对象类型：Integer Double String 自定义类型 List Map
    * 返回的不是逻辑视图的名称，而直接就是数据返回
    * 一般是ajax请求搭配使用 ，将json格式的数据直接返回给响应体
    * */
    /*3.1返回基础类型*/
    @ResponseBody
    @RequestMapping("test3_1.do")
    public Integer test3_1(){
        return 666;
    }

    @ResponseBody
    @RequestMapping("test3_2.do")
    public String test3_2(){
        return "test3_2---OK";
    }

    /*
    3.2 返回自定义对象类型
    */
    @ResponseBody
    @RequestMapping("test3_3.do")
    public Team test3_3(){
        Team team = new Team();
        team.setLocation("迈阿密");
        team.setTeamId(1006);
        team.setTeamName("热火");

        return team;
    }

    /*
    3.3 返回集合List
    */
    @ResponseBody
    @RequestMapping("test3_4.do")
    public List<Team> test3_4(){
        List<Team> list = new ArrayList<>();
        for(int i=1;i<=5;i++){
            Team team = new Team();
            team.setTeamId(1009+i);
            team.setTeamName("湖人"+i);
            team.setLocation("洛杉矶"+i);

            list.add(team);
        }

        return list;
    }

    /*
    3.4 返回集合Map
    */
    @ResponseBody
    @RequestMapping("test3_5.do")
    public Map<String,Team> test3_5(){

        Map<String,Team> map = new HashMap<>();
        for(int i=1;i<=5;i++){
            Team team = new Team();
            team.setTeamId(1000+i);
            team.setTeamName("勇士"+i);
            team.setLocation("金州"+i);
            team.setCreateTime(new Date());

            map.put(team.getTeamId()+"",team);
        }

        return map;
    }

    /*
    4.无返回值
    * */
    //通过 HttpServletResponse 给出响应
    @RequestMapping("test4_3.do")
    public void test4_3(HttpServletResponse response) throws IOException {
        //response.setCharacterEncoding("UTF-8");此行可不用
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        writer.write("<html>\n" +
                "<head>\n" +
                "    <title>test4无返回值</title>\n" +
                "</head>\n" +
                "<body>" +
                "<h1>--------------test4-3-result--------------</h1>" +
                "返回void类型测试---直接返回字符串" +
                "</body>\n" +
                "</html>");
        writer.flush();
        writer.close();
    }

}
